package com.example.dessertstoressm.controller;


import com.example.dessertstoressm.Service.AccountService;
import com.example.dessertstoressm.Service.CatalogService;
import com.example.dessertstoressm.domain.Account;
import com.example.dessertstoressm.domain.Product;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.io.IOException;
import java.util.List;

@Controller
public class editAccountController {


    private String msg_password;
    private String msg_repeated_password;
    private String msg_firstname;
    private String msg_lastname;
    private String msg_email;
    private String msg_phone;
    private String msg_address1;
    private String msg_address2;
    private String msg_city;
    private String msg_state;
    private String msg_zip;
    private String msg_country;


    public Account account = new Account();
    @Autowired
    public AccountService accountService;

    @Autowired
    public CatalogService catalogService;

    @RequestMapping("editAccountForm")
    public String myAccountInformation(HttpServletRequest req){
        HttpSession session = req.getSession();
        Account account = (Account) session.getAttribute("account");

        return "account/editAccount";
    }

    @PostMapping("editAccount")
    public String editAccountInformation(HttpServletRequest req, Model model) throws IOException {
        HttpSession session = req.getSession();

        Account account1 = (Account) session.getAttribute("account");
        String name = account1.getUsername();
        Account loginAccount = accountService.getAccountByUsername(name);
        if(loginAccount==null){
            System.out.println("没得到用户");
        }else{
            System.out.println(loginAccount.getSignOn().getPassword());
        }


        account.setUsername(name);
        account.getSignOn().setPassword(req.getParameter("password"));
        account.setFirstName(req.getParameter("account.firstName"));
        account.setLastName(req.getParameter("account.lastName"));
        account.setEmail(req.getParameter("account.email"));
        account.setPhone(req.getParameter("account.phone"));
        account.setAddress1(req.getParameter("account.address1"));
        account.setAddress2(req.getParameter("account.address2"));
        account.setCity(req.getParameter("account.city"));
        account.setState(req.getParameter("account.state"));
        account.setZip(req.getParameter("account.zip"));
        account.setCountry(req.getParameter("account.country"));
        account.getProfile().setLanguagePreference(req.getParameter("account.languagePreference"));
        account.getBannerData().setFavouriteCategoryId(req.getParameter("account.favouriteCategoryId"));
        account.getProfile().setListOption(req.getParameter("account.listOption") != null);
        account.getProfile().setBannerOption(req.getParameter("account.bannerOption") != null);

        if(account.getSignOn().getPassword() == null || account.getSignOn().getPassword().equals("")){
            account.getSignOn().setPassword(loginAccount.getSignOn().getPassword());
        }
        else {
            //检查两次密码
            String password = req.getParameter("password");
            String repeatPwd = req.getParameter("repeatedPassword");
            if(!password.equals(repeatPwd)){
                //message
                session.setAttribute("msg_repeated_password","两次密码不相同");

                return "account/editAccount";
            }
        }

        //判断是否填写正确
        if(!validate()){
            model.addAttribute("msg_password", this.msg_password);
            model.addAttribute("msg_firstname", this.msg_firstname);
            model.addAttribute("msg_lastname", this.msg_lastname);
            model.addAttribute("msg_email", this.msg_email);
            model.addAttribute("msg_phone", this.msg_phone);
            model.addAttribute("msg_address1", this.msg_address1);
            model.addAttribute("msg_address2", this.msg_address2);
            model.addAttribute("msg_city", this.msg_city);
            model.addAttribute("msg_state", this.msg_state);
            model.addAttribute("msg_zip", this.msg_zip);
            model.addAttribute("msg_country", this.msg_country);

            return "account/editAccount";
        }
        else {
            accountService.updateAccount(account);
            account.getSignOn().setPassword(null);
            session.setAttribute("account" , account);


            List<Product> myList = catalogService.getProductListByCategory(account.getBannerData().getFavouriteCategoryId());
            session.setAttribute("myList", myList);

            System.out.println("进入修改成功");
            req.setAttribute("editMsg", "信息修改成功");
            model.addAttribute("editMsg", "信息修改成功");

            return "account/editAccount";
        }

    }


    private boolean validate(){
        if(passwordIsRight() & firstnameIsRight() & lastnameIsRight() & emailIsRight() &
                phoneIsRight() & address1IsRight() & address2IsRight() & cityIsRight() &
                stateIsRight() & zipIsRight() & countryIsRight()){
            return true;
        } else {
            return false;
        }
    }

    private boolean passwordIsRight(){
        if(account.getSignOn().getPassword() == null || account.getSignOn().getPassword().equals("")){
            return true;
        }
        if(account.getSignOn().getPassword().length()<6||account.getSignOn().getPassword().length()>19){
            this.msg_password = "密码长度需为6~20位";
            return false;
        }
        return true;
    }

    private boolean firstnameIsRight(){
        if(account.getFirstName() == null || account.getFirstName().equals("")){
            this.msg_firstname = "名不能为空";
            return false;
        }
        return true;
    }

    private boolean lastnameIsRight(){
        if(account.getLastName() == null || account.getLastName().equals("")){
            this.msg_lastname = "姓不能为空";
            return false;
        }
        return true;
    }

    private boolean emailIsRight(){
        if(account.getEmail() == null || account.getEmail().equals("")){
            this.msg_email = "邮箱不能为空";
            return false;
        }
        return true;
    }

    private boolean phoneIsRight(){
        if(account.getPhone() == null || account.getPhone().equals("")){
            this.msg_phone = "电话不能为空";
            return false;
        }
        return true;
    }

    private boolean address1IsRight(){
        if(account.getAddress1() == null || account.getAddress1().equals("")){
            this.msg_address1 = "地址1不能为空";
            return false;
        }
        return true;
    }

    private boolean address2IsRight(){
        if(account.getAddress2() == null || account.getAddress2().equals("")){
            this.msg_address2 = "地址2不能为空";
            return false;
        }
        return true;
    }

    private boolean cityIsRight(){
        if(account.getCity() == null || account.getCity().equals("")){
            this.msg_city = "城市不能为空";
            return false;
        }
        return true;
    }

    private boolean stateIsRight(){
        if(account.getState() == null || account.getState().equals("")){
            this.msg_state = "州不能为空";
            return false;
        }
        return true;
    }

    private boolean zipIsRight(){
        if(account.getZip() == null || account.getZip().equals("")){
            this.msg_zip = "邮编不能为空";
            return false;
        }
        return true;
    }

    private boolean countryIsRight(){
        if(account.getCountry() == null || account.getCountry().equals("")){
            this.msg_country = "国家不能为空";
            return false;
        }
        return true;
    }

}
